from datetime import datetime, timedelta

import chinese_calendar as calendar


def get_workdays_until_date(target_date):
    """
    获取从月份的第一天到指定日期的工作日天数。

    :param target_date: 目标日期 (datetime 对象)
    :return: 工作日天数
    """
    # 获取指定日期所在的年份和月份
    year = target_date.year
    month = target_date.month

    # 获取本月第一天
    first_day = datetime(year, month, 1)

    # 计算从第一天到目标日期之间的工作日天数
    workdays = 0
    current_day = first_day
    while current_day <= target_date:
        if calendar.is_workday(current_day):
            workdays += 1
        current_day += timedelta(days=1)

    return workdays


def get_workdays(start: str, end: str) -> int:
    """
    获取指定日期端的工作日天数。
    :param start: 目标日期 (yyyy-MM-dd 对象)
    :param end: 目标日期 (yyyy-MM-dd 对象)
    :return: 工作日天数
    """

    from_day = datetime.strptime(start, "%Y-%m-%d")
    end_date = datetime.strptime(end, "%Y-%m-%d")

    # 获取本月第一天
    first_day = from_day

    # 计算从第一天到目标日期之间的工作日天数
    workdays = 0
    current_day = first_day
    while current_day <= end_date:
        if calendar.is_workday(current_day):
            workdays += 1
        current_day += timedelta(days=1)

    return workdays


def get_workdays_in_month(year, month, day=None):
    """
    获取指定年份和月份的工作日天数，支持计算截至到指定日期的工作日天数。

    :param year: 年份
    :param month: 月份
    :param day: 截止日期中的天，默认为该月的最后一天
    :return: 工作日天数
    """
    # 获取本月第一天
    first_day = datetime(year, month, 1)

    # 确定截止日期
    if day is None:
        # 如果没有提供 day，则默认为该月的最后一天
        if month == 12:
            end_date = datetime(year + 1, 1, 1) - timedelta(days=1)
        else:
            end_date = datetime(year, month + 1, 1) - timedelta(days=1)
    else:
        # 创建指定的截止日期
        end_date = datetime(year, month, day)

    # 计算工作日天数
    workdays = 0
    current_day = first_day
    while current_day <= end_date:
        if calendar.is_workday(current_day):
            workdays += 1
        current_day += timedelta(days=1)

    return workdays


def count_working_saturdays(start_date, end_date):
    # 需要工作的周六日期列表
    working_saturdays = [
        "2025-01-11",
        "2025-01-18",
        "2025-02-22",
        "2025-03-08",
        "2025-03-22",
        "2025-04-12",
        "2025-04-19",
        "2025-04-27",
        "2025-05-10",
        "2025-05-24",
        "2025-06-07",
        "2025-06-21",
        "2025-07-05",
        "2025-07-19",
        "2025-08-02",
        "2025-08-16",
        "2025-08-30",
        "2025-09-13",
        "2025-09-20",
        "2025-10-11",
        "2025-10-18",
        "2025-11-01",
        "2025-11-15",
        "2025-11-29",
        "2025-12-13",
        "2025-12-27",
    ]
    # 将字符串转为 datetime 对象
    working_saturdays = [
        datetime.strptime(date, "%Y-%m-%d") for date in working_saturdays
    ]
    """计算在给定时间段内的需要工作的周六总数"""
    start_date = datetime.strptime(start_date, "%Y-%m-%d")
    end_date = datetime.strptime(end_date, "%Y-%m-%d")

    # 计算在时间范围内的需要工作的周六
    return sum(start_date <= date <= end_date for date in working_saturdays)
